{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 统计数量&数量最多的前几个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({'look': 2, 'my': 2, 'eyes': 2, 'into': 1})\n",
      "[('look', 2), ('my', 2), ('eyes', 2)]\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "\n",
    "words = [\n",
    "    'look', 'into', 'my', 'eyes', 'look', 'my', 'eyes',\n",
    "]\n",
    "\n",
    "word_counts = Counter(words)\n",
    "top_three = word_counts.most_common(3)\n",
    "print(word_counts)\n",
    "print(top_three)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 额外增加计数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({'eyes': 3, 'look': 2, 'my': 2, 'into': 1})\n",
      "Counter({'eyes': 4, 'look': 3, 'my': 3, 'into': 1})\n"
     ]
    }
   ],
   "source": [
    "word_counts['eyes'] += 1        # 直接相加\n",
    "print(word_counts)              # update\n",
    "morewords = ['look', 'my', 'eyes', 'at']\n",
    "word_counts.update(morewords)\n",
    "print(word_counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Counter对象之间可进行数学运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({'look': 2, 'my': 2, 'eyes': 2, 'into': 1})\n",
      "Counter({'look': 1, 'my': 1, 'eyes': 1})\n",
      "Counter({'look': 3, 'my': 3, 'eyes': 3, 'into': 1})\n",
      "Counter({'look': 1, 'into': 1, 'my': 1, 'eyes': 1})\n"
     ]
    }
   ],
   "source": [
    "a = Counter(words)\n",
    "b = Counter(morewords)\n",
    "c = a+b\n",
    "d = a-b\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.4 64-bit",
   "language": "python",
   "name": "python37464bit0eab7a6caf29454899b876d50238c56e"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
